package com.mproject.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mproject.message.MessageUtil;
import com.mproject.message.ServerConfiguration;
import com.project.connection.DatabaseConnection;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

//Sets the path to base URL
@Path("/del")
public class Del {
	public static Connection con = DatabaseConnection.getConnection();

	// DEL A TASK
	@GET
	@Path("/task")
	@Produces("text/plain")
	public String delTask(@QueryParam("id") int id,
			@QueryParam("nusnet") String nusnet) {
		String result = "{\"#error\":[";

		String project_name = "";
		String project_id = "";
		long due = 0;
		String title = "";
		try {
			Statement stmt = con.createStatement();
			String query = "select * from work where id=" +id+ "";
			ResultSet rs = stmt.executeQuery(query);						
			while(rs.next()) {
				project_name = rs.getString("project_name");
				project_id = rs.getString("project_id");
				due = rs.getLong("due");
				title = rs.getString("title");
			}
		} catch (Exception e) {       
			System.out.println(e);
		}

		try {
			Statement stmt = con.createStatement();
			String update = "delete from work where id = " +id+ "";
			stmt.executeUpdate(update);

			result += "false]}";
		} catch (Exception e) {
			System.out.println(e);
			result += "true]}";
		}

		String send_message = "not{\"message\":{\"cat\":\"del_task\",\"project_name\":\""+project_name+"\",\"start\":"+due+",\"title\":"+title+"}}";
		try {
			Statement stmt = con.createStatement();
			String query = "select u.mac from user u, project p where u.nusnet = p.nusnet and u.nusnet <> '" +nusnet+ "' and p.project_id = '" +project_id+ "';";
			ResultSet rs = stmt.executeQuery(query);
			while(rs.next()) {
				MessageUtil.sendMessage(ServerConfiguration.AUTHENTICATION_TOKEN,rs.getString("mac"),send_message);
			}

		} catch (Exception e) {
			System.out.println(e);
		}	

		return result;
	}

	// DEL A MEETING
	@GET
	@Path("/meeting")
	@Produces("text/plain")
	public String delMeeting(@QueryParam("id") int id,
			@QueryParam("nusnet") String nusnet) {
		String result = "{\"#error\":[";

		String project_name = "";
		String project_id = "";
		long start = 0;
		String title = "";
		try {
			Statement stmt = con.createStatement();
			String query = "select * from work where id=" +id+ "";
			ResultSet rs = stmt.executeQuery(query);						
			while(rs.next()) {
				project_name = rs.getString("project_name");
				project_id = rs.getString("project_id");
				start = rs.getLong("start");
				title = rs.getString("title");
			}
		} catch (Exception e) {       
			System.out.println(e);
		}

		try {
			Statement stmt = con.createStatement();
			String update = "delete from work where id = " +id+ "";
			stmt.executeUpdate(update);

			result += "false]}";
		} catch (Exception e) {
			System.out.println(e);
			result += "true]}";
		}

		String send_message = "not{\"message\":{\"cat\":\"del_meeting\",\"project_name\":\""+project_name+"\",\"start\":"+start+",\"title\":"+title+"}}";
		try {
			Statement stmt = con.createStatement();
			String query = "select u.mac from user u, project p where u.nusnet = p.nusnet and u.nusnet <> '" +nusnet+ "' and p.project_id = '" +project_id+ "';";
			ResultSet rs = stmt.executeQuery(query);
			while(rs.next()) {
				MessageUtil.sendMessage(ServerConfiguration.AUTHENTICATION_TOKEN,rs.getString("mac"),send_message);
			}

		} catch (Exception e) {
			System.out.println(e);
		}	

		return result;
	}

	// DEL A PLAN
	@GET
	@Path("/plan")
	@Produces("text/plain")
	public String delPlan(@QueryParam("id") int id) {
		String result = "{\"#error\":[";

		try {
			Statement stmt = con.createStatement();
			String update = "delete from plan where id = " +id+ "";
			stmt.executeUpdate(update);

			result += "false]}";
		} catch (Exception e) {
			System.out.println(e);
			result += "true]}";
		}
		return result;
	}

	// REMOVE ALL BUSY HOURS
	@GET
	@Path("/busy")
	@Produces("text/plain")
	public String delBusyHours(@QueryParam("project_id") String project_id,
			@QueryParam("date") long date,
			@QueryParam("nusnet") String nusnet) {
		String result = "{\"#error\":[";

		try {
			Statement stmt = con.createStatement();
			String update = "delete from busy where project_id = '" +project_id+ "' and date = " +date+ " and nusnet = '" +nusnet+ "';";
			stmt.executeUpdate(update);

			result += "false]}";
		} catch (Exception e) {
			System.out.println(e);
			result += "true]}";
		}
		return result;
	}
}